/**   
 * @{#} ClimbByStage.java Create on 2013-7-23 下午3:22:34   
 *   
 * Copyright (c) 2012 by Baidu.   
 */
package union.func;

import java.util.HashMap;
import java.util.List;

import objtable.core.IntKeyGen;
import objtable.core.TableGenerator;
import objtable.port.DbPort;
import objtable.struct.DataType;
import objtable.struct.Row;

/**
 * @author <a href="mailto:zhangbo07@baidu.com">zhangbo07</a>
 * @version 1.0
 */

public class DomainHangYeRate {

	protected HashMap<String, Stages> hm = new HashMap<String, Stages>();

	public DomainHangYeRate() {
	}

	public DomainHangYeRate(DbPort db) {
		init(db, 2);
	}

	protected void init(DbPort db, int type) {
		// 网盟爬坡表
		TableGenerator tg = new TableGenerator("zhiying" + IntKeyGen.nextInt())
				.addField(DataType.INT, "rootid")
				.addField(DataType.INT, "subid").addField(DataType.LONG, "low")
				.addField(DataType.LONG, "high")
				.addField(DataType.DOUBLE, "divide")
				.addField(DataType.DOUBLE, "basevalue");
		List<Row> r = db
				.fromDB(tg,
						"select rootid,subid,low,high,divide,basevalue from zhiyingdivideruledesc where biztype="
								+ type);
		// 注册网盟爬坡函数
		initRow(r);
	}

	private void initRow(List<Row> rows) {
		for (Row r : rows) {
			String key = r.getInt("rootid") + "@" + r.getInt("subid");
			if (!hm.containsKey(key)) {
				Stages s = new Stages();
				hm.put(key, s);
			}
			hm.get(key).addStage(r.getLong("low"), r.getLong("high"),
					r.getDouble("divide"), r.getDouble("basevalue"));
		}
	}

	public double domainhangyerate(int rootid, int subid, long value) {
		String key = rootid + "@" + subid;
		Stages s = hm.get(key);
		return s.queryRate(value);
	}
}
